package cn.onesdream.app.api.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.nio.charset.Charset;

@Configuration
public class HttpMessageConvertersConfig {
    @Bean
    public HttpMessageConverters configureMessageConverters() {
        //创建fastJson消息转换器
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //创建配置类
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //修改配置返回内容的过滤
        fastJsonConfig.setSerializerFeatures(
                //key和value使用双引号输出
                SerializerFeature.QuoteFieldNames,
                //消除循环引用
                SerializerFeature.DisableCircularReferenceDetect,
                //输出为null的字段
                SerializerFeature.WriteMapNullValue,
                //将为null的字段的值显示为“”
//                SerializerFeature.WriteNullStringAsEmpty,
                //Boolean类型为null时输出false
                SerializerFeature.WriteNullBooleanAsFalse,
                //格式化输出
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteDateUseDateFormat
        );
        fastConverter.setDefaultCharset(Charset.forName("UTF-8"));
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }
}
